package com.porterhead.account.resource;

import com.porterhead.account.AccountService;
import com.porterhead.account.api.ApiAccount;
import com.porterhead.resource.BaseResource;
import com.porterhead.user.User;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.security.RolesAllowed;
import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.SecurityContext;

@Path("/v1.0/account")
@Component
@Produces({MediaType.APPLICATION_JSON})
@Consumes({MediaType.APPLICATION_JSON})
public class AccountResource extends BaseResource {

    private AccountService accountService;

    @Autowired
    public AccountResource(AccountService accountService) {
        this.accountService = accountService;
    }

    @RolesAllowed({"ROLE_USER"})
    @Path("{accountId}")
    @GET
    public ApiAccount getAccount(@PathParam("accountId") String accountId, @Context SecurityContext securityContext) {
        User requestingUser = loadUserFromSecurityContext(securityContext);
        return accountService.getAccount(requestingUser.getId(), accountId);
    }
}
